package controller;

import dao.CountryMapper;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import model.Country;
import model.Page;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.test.web.ModelAndViewAssert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.view.InternalResourceView;
import service.CountryService;
import service.Impl.CountryServiceImpl;

import javax.servlet.http.HttpServletRequest;

/**
 * Create by IntelliJ IDEA
 *
 * @author: jsonor
 * @date-Time: 2018/3/20 16:09
 * @description:
 */
@Controller
public class CountryMessageController {
  private View view;
  @Autowired
  private CountryService countryService;



  @RequestMapping("/findAll")
  public ModelAndView findAll()
{
  List<Country> allCountry = countryService.findAllCountry();
  System.out.println(allCountry);
  ModelAndView modelAndView=new ModelAndView();
  modelAndView.setView(new InternalResourceView("/WEB-INF/view/showCountryList.jsp"));
  modelAndView.addObject("list",allCountry);
  return modelAndView;
}




  @RequestMapping("/deleteCountry")
  public View deleteCountry(HttpServletRequest request)
  {
    String cname = request.getParameter("cname");
    int num = countryService.deleteByCode(cname);
    System.out.println(num);
    view = new InternalResourceView("/app/findAll");
    return view;
  }


  @RequestMapping("/showCountryMessage")
  public ModelAndView showCountryMessage (HttpServletRequest request)
  {
    String cname = request.getParameter("cname");
    Country country = countryService.findByName(cname);
    System.out.println(country);
    ModelAndView modelAndView = new ModelAndView();
    modelAndView.setView(new InternalResourceView("/WEB-INF/view/CountryMessage.jsp"));
    modelAndView.addObject("c", country);
    return modelAndView;
  }


  @RequestMapping("/addCountryMessage")
  public View addCountryMessage(HttpServletRequest request)
  {
    Country country = new Country();
    try {
      BeanUtils.populate(country, request.getParameterMap());
      int num = countryService.addCountry(country);
      System.out.println(num);
    } catch (Exception e) {
      e.printStackTrace();
    }
    view = new InternalResourceView("/app/findAll");
    return view;
  }


  @RequestMapping("/sendAddMessage")
  public View sendAddMessage()
  {
    view=new InternalResourceView("/WEB-INF/view/addCountryMessage.jsp");
    return view;
  }


  @RequestMapping("/updateCountryMessage")
  public View updateCountryMessage(HttpServletRequest request) {
    String cname = request.getParameter("cname");
    Country country = new Country();
    try {
      BeanUtils.populate(country, request.getParameterMap());
      int num = countryService.updataCountry(country);
    } catch (Exception e) {
      e.printStackTrace();
    }
    view = new InternalResourceView("/app/findAll");
    return view;
  }


  @RequestMapping("/searchCountry")
  public ModelAndView searchCountry(HttpServletRequest request) {
    String name = request.getParameter("name");
    List<Country> country = countryService.searchCountryByName(name);
    System.out.println(country.size());
    ModelAndView modelAndView = new ModelAndView();
    modelAndView.addObject("list", country);
    modelAndView.setView(new InternalResourceView("/WEB-INF/view/showCountryList.jsp"));
    return modelAndView;
  }

  @RequestMapping("/pageBean")
  public ModelAndView pageBean(HttpServletRequest request) {
    int page = Integer.valueOf(request.getParameter("currentPage"));
    int pageSize = 10;
    List<Country> list = countryService.findAllCountry();
    List<Country> pageList = countryService.findAllByPage(page, pageSize);
    Page pageBean = new Page();
    pageBean.setCurrentPage(page);
    pageBean.setPageList(pageList);
    pageBean.setPageSize(pageSize);
    pageBean.setCount(list.size());
    ModelAndView view = new ModelAndView();
    view.addObject("page", pageBean);
    view.setView(new InternalResourceView("/WEB-INF/view/pageCountry.jsp"));
    return view;
  }
}
